package main

import (
	"fmt"
	"sync"
)

// 互斥锁
var (
	counter int
	mutex   sync.Mutex
)

func worker(id int, wg *sync.WaitGroup) {
	defer wg.Done()
	mutex.Lock()   // 加锁
	counter++      // 关键区代码
	mutex.Unlock() // 解锁
}

func main() {
	var wg sync.WaitGroup
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go worker(i, &wg)
	}
	wg.Wait()
	fmt.Println("Final Counter:", counter)
}
